package com.shaoshaohuo.app.ui.fragment;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.PathMeasure;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.HomeListViewAdapter;
import com.shaoshaohuo.app.adapter.MaxImagerViewPager;
import com.shaoshaohuo.app.adapter.MyViewPagerAdapter;
import com.shaoshaohuo.app.constant.ShareKey;
import com.shaoshaohuo.app.entity.Home_Hot_Entity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.NewDetailsActivity;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.view.SelfadaptionImageview;
import com.shaoshaohuo.app.view.Vpindicator;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 热销
 * Created by Administrator on 2017/6/6.
 */

public class HomeHotFragment extends Fragment implements AdapterView.OnItemClickListener, HomeListViewAdapter.AdapterListener {
    private String url;
    private View view;
    private View inflate;
    private ListView home_fragment_listView;
    private HomeListViewAdapter home_listView_adapter;
    private Home_Hot_Entity home_hot_entity;
    private List<Home_Hot_Entity.DataBean.HotListBean> hotlists;
    private MaxImagerViewPager home_fragment_viewPager;
    private List<View> home_viewPager_picture;
    private MyViewPagerAdapter viewPagerAdapter;
    private MaxImagerViewPager.Myadapter viewPager_adapter;
    //    private int[] picture = new int[]{R.mipmap.backgroud, R.mipmap.logo, R.mipmap.gouwuche};
    private RelativeLayout home_relativeLayout;
    private Handler handler = new Handler();
    private MaxImagerViewPager maxImagerViewPager;
    private Vpindicator vpd;

    //    public static HomeTabFragment newInstance(int page) {
//        Bundle args = new Bundle();
//        args.putInt(ARG_PAGE, page);
//        HomeTabFragment pageFragment = new HomeTabFragment();
//        pageFragment.setArguments(args);
//        return pageFragment;
//    }
    private SwipeRefreshLayout home_hot_swip;

    public HomeHotFragment() {

    }

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_fragment_page, container, false);
        url = getArguments().getString("url");
//        Toast.makeText(getActivity(), url, Toast.LENGTH_SHORT).show();
        initView();
        decideBanner();
//        getData();//假数据
        bindAdapter();
        RequestData();
        initListener();
        return view;
    }

    private void initListener() {
        home_fragment_listView.setOnItemClickListener(this);
        home_listView_adapter.setHomeAdapterListener(this);
        home_hot_swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                RequestData();
                home_hot_swip.setRefreshing(false);
            }
        });
    }


    private void decideBanner() {
//        if (url.equals(HttpConfig.SubUrl.HOT_SALE)) {
//            maxImagerViewPager.setVisibility(View.VISIBLE);
//        } else {
//            maxImagerViewPager.setVisibility(View.GONE);
//        }
    }

    private List<View> getData(List<Home_Hot_Entity.DataBean.FocusBean> focus) {
        home_viewPager_picture = new ArrayList<>();
        for (int j = 0; j < focus.size(); j++) {
            SelfadaptionImageview imageView = new SelfadaptionImageview(getActivity());
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(getActivity()).load(focus.get(j).getBigimg()).into(imageView);
            home_viewPager_picture.add(imageView);
        }

        return home_viewPager_picture;
    }

    //网络请求
    private void RequestData() {
        ShopHttpConfig.ecmain(getContext(), url, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                home_hot_entity = (Home_Hot_Entity) o;
                hotlists = home_hot_entity.getData().getHotList();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        home_listView_adapter.setdate(hotlists);
                        List<View> data = getData(home_hot_entity.getData().getFocus());
                        home_fragment_viewPager.setMaXadapter(data, getActivity());
                        Log.e("TAG", "刷新");
                        vpd.setvp(home_fragment_viewPager);
                        vpd.setnum(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.lunbo_a), BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.lunbo_b), data.size(), (int) getContext().getResources().getDimension(R.dimen.imamarn1));
                    }
                });
            }
        }, Home_Hot_Entity.class);
    }

    private void bindAdapter() {
        home_fragment_listView.setAdapter(home_listView_adapter);
    }

    private void initView() {
        home_fragment_listView = (ListView) view.findViewById(R.id.home_fragment_listView);
        hotlists = new ArrayList<>();
        home_listView_adapter = new HomeListViewAdapter(getActivity(), hotlists);

        inflate = LayoutInflater.from(getActivity()).inflate(R.layout.home_viewpager, null);
        home_fragment_viewPager = (MaxImagerViewPager) inflate.findViewById(R.id.home_lunboViewPager);
        vpd = (Vpindicator) inflate.findViewById(R.id.vpd);
        home_fragment_listView.addHeaderView(inflate);
        //750 *415
        float i = ((float) getResources().getDisplayMetrics().widthPixels / 750.00f);
        home_fragment_viewPager.setLayoutParams(new RelativeLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (415 * i)));
        home_relativeLayout = (RelativeLayout) view.findViewById(R.id.home_relativeLayout);

        home_hot_swip = (SwipeRefreshLayout) view.findViewById(R.id.home_hot_swip);
    }

    @Override
    public void onResume() {
        super.onResume();
//        home_fragment_viewPager.startlop();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(getActivity(), "跳转", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getContext(), NewDetailsActivity.class);
        Log.e("首页", position + "==" + hotlists.size());
        String catid = hotlists.get(position - 1).getCatid();
        intent.putExtra(ShareKey.CATID, catid);
        intent.putExtra("imgurl", hotlists.get(position - 1).getImg());
        startActivity(intent);
    }


    private ValueAnimator valueAnimator;
    private float[] pos = new float[2];
    private float[] tan = new float[2];

    @Override
    public void pathmove(final PathMeasure pathMeasure, float x, float y) {
        final ImageView view = new ImageView(getContext());
        view.setImageResource(R.mipmap.round_red);
        view.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//        conten.addView(view, conten.getChildCount() - 1);
        home_relativeLayout.addView(view);
        valueAnimator = ValueAnimator.ofFloat(0, pathMeasure.getLength());
        float length = pathMeasure.getLength();
        Log.e("TAG", "pathmove: " + length);
        long duration = (long) ((long) length > 200 ? length -= 200 : length);
        valueAnimator.setDuration(duration);
        valueAnimator.setRepeatCount(0);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float animatedValue = (Float) animation.getAnimatedValue();
                pathMeasure.getPosTan(animatedValue, pos, tan);
                float x = pos[0];
                float y = pos[1];
                view.setTranslationX(x);
                view.setTranslationY(y);
                Log.d("TAG", "onAnimationUpdate: " + x + "y" + y + "float" + animatedValue);
//                Newclassificationshaopdataadapter.this.view.layout((int) x, (int) y, Newclassificationshaopdataadapter.this.view.getWidth() + (int) x, Newclassificationshaopdataadapter.this.view.getHeight() + (int) y);
//                Newclassificationshaopdataadapter.this.view.setTranslationX(x);
//                Newclassificationshaopdataadapter.this.view.setTranslationY(y);

            }
        });
        valueAnimator.start();
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                home_relativeLayout.removeView(view);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                home_relativeLayout.removeView(view);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }

}
